package com.myBase.core.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.StringWriter;
import java.io.Writer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;

public class StringUtil {
    private static Logger LOG = Logger.getLogger(StringUtil.class);

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0 || str.trim().length() == 0) {
            return true;
        }
        return false;
    }

    public static String NullToEmpty(Object o) {
        if (o == null || "null".equals(o.toString()))
            return "";
        else
            return o.toString();
    }

    // MD5加密
    public static String md5(String source) {
        if (source == null) {
            return null;
        }
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source.getBytes());
            byte tmp[] = md.digest();
            char str[] = new char[16 * 2];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            LOG.error("A MD5 exception is occur.");
            return null;
        }
    }

    // json字符串转换成java对象
    public static <T> T json2Object(String json, Class<T> c) {
        T o = null;
        try {
            o = new ObjectMapper().readValue(json, c);
        } catch (Exception e) {
            LOG.error(e);
        }
        return o;
    }

    // java对象转换成json字符串
    public static String object2JSON(Object o) {
        ObjectMapper om = new ObjectMapper();
        Writer w = new StringWriter();
        String json = null;
        try {
            om.writeValue(w, o);
            json = w.toString();
            w.close();
        } catch (Exception e) {
            LOG.error(e);
        }
        return json;
    }

    /**
     * 返回byte的数据大小对应的文本
     * 
     * @param size
     * @return
     */
    public static String getDataSize(long size) {
        DecimalFormat formater = new DecimalFormat("####.00");
        if (size < 1024) {
            return size + "bytes";
        } else if (size < 1024 * 1024) {
            float kbsize = size / 1024f;
            return formater.format(kbsize) + "KB";
        } else if (size < 1024 * 1024 * 1024) {
            float mbsize = size / 1024f / 1024f;
            return formater.format(mbsize) + "MB";
        } else if (size < 1024 * 1024 * 1024 * 1024) {
            float gbsize = size / 1024f / 1024f / 1024f;
            return formater.format(gbsize) + "GB";
        } else {
            return "size: error";
        }
    }

    // 验证码相关
    public static int WIDTH = 65;// 设置图片的宽度

    public static int HEIGHT = 22;// 设置图片的高度

    public static void drawBackground(Graphics g) {
        g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, WIDTH, HEIGHT);
        for (int i = 0; i < 120; i++) {
            int x = (int) (Math.random() * WIDTH);
            int y = (int) (Math.random() * HEIGHT);
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            g.setColor(new Color(red, green, blue));
            g.drawOval(x, y, 1, 0);
        }
    }

    public static void drawRands(Graphics g, char[] rands) {
        Random random = new Random();
        int red = random.nextInt(110);
        int green = random.nextInt(50);
        int blue = random.nextInt(50);
        g.setColor(new Color(red, green, blue));
        g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
        g.drawString("" + rands[0], 1, 17);
        g.drawString("" + rands[1], 16, 15);
        g.drawString("" + rands[2], 31, 18);
        g.drawString("" + rands[3], 46, 16);
    }

    public static char[] generateCheckCode() {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] rands = new char[4];
        for (int i = 0; i < 4; i++) {
            int rand = (int) (Math.random() * 36);
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }

    public static String moneyFormat(Double money) {
        NumberFormat nf = new DecimalFormat("#,##0.00#元");// NumberFormat.getCurrencyInstance(Locale.CHINA);//
        String moneyStr = nf.format(money);
        return moneyStr;
    }

    /**
     * 去掉样式字段
     * 
     * @return
     */
    public static String removeStyleChar(String styleStr) {
        if (styleStr == null)
            return null;
        char[] cs = styleStr.toCharArray();
        StringBuilder sb = new StringBuilder("");
        boolean b = true;
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] == '<') {
                b = false;
                continue;
            }
            if (cs[i] == '>') {
                b = true;
                continue;
            }
            if (b) {
                sb.append(cs[i]);
            }
        }
        int len = sb.length();

        sb = new StringBuilder(sb.toString().replaceAll("马上摇奖。", ""));
        int index = sb.indexOf("民生易贷团队");
        if (index > 0) {
            return sb.substring(0, index);
        } else {
            return sb.substring(0, len);
        }

        // if(styleStr==null) return null;
        // styleStr=styleStr.replaceAll("<span>", "");
        // styleStr=styleStr.replaceAll("</a>", "");
        // styleStr=styleStr.replaceAll("</div>", "");
        // styleStr=styleStr.replaceAll("<br/>", "");
        //
        // StringBuilder sb=new StringBuilder(styleStr2);
        // while(styleStr.indexOf("<div")>=0){
        // int start=styleStr.indexOf("<div");
        // int end=styleStr.indexOf(">");
        // styleStr=styleStr.substring(0,
        // start)+""+styleStr.substring(end+1,styleStr.length());
        // }
    }

    /**
     * 金额格式化
     * 
     * @param s
     *            金额
     * @param len
     *            小数位数
     * @return 格式后的金额
     */
    public static String formatMoney(String s, int len) {
        if (s == null || s.length() < 1) {
            return "";
        }
        NumberFormat formater = null;
        double num = Double.parseDouble(s);
        if (len == 0) {
            formater = new DecimalFormat("###,##0.00");

        } else {
            StringBuffer buff = new StringBuffer();
            buff.append("###,##0.00");
            for (int i = 0; i < len; i++) {
                buff.append("#");
            }
            formater = new DecimalFormat(buff.toString());
        }
        return formater.format(num);
    }

    /**
     * 去除 地区 名称 所带的'省'、'自治区'、'市' 后缀
     * 
     * @param name
     * @return
     */
    public static String parseAreaName(String areaName) {
        if (isEmpty(areaName)) {
            return areaName;
        }
        int index = areaName.indexOf("省");
        if (index < 0) {
            index = areaName.indexOf("自治区");
        }
        if (index < 0) {
            index = areaName.indexOf("市");
        }
        if (index < 0) {
            return areaName;
        }
        return areaName.substring(0, index);
    }

    /**
     * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
     * 
     * @author zl
     * @param obj
     * @return
     */
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null)
            return true;

        if (obj instanceof CharSequence)
            return ((CharSequence) obj).length() == 0;

        if (obj instanceof Collection)
            return ((Collection) obj).isEmpty();

        if (obj instanceof Map)
            return ((Map) obj).isEmpty();

        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    /**
     * 校验银行卡卡号
     * 
     * @param cardId
     * @return
     */
    public static boolean checkBankCardNo(String bankCardNo) {
        if (bankCardNo == null || bankCardNo.length() == 0 || !bankCardNo.matches("\\d+")) {
            return false;
        }
        char bit = getBankCardCheckCode(bankCardNo.substring(0, bankCardNo.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return bankCardNo.charAt(bankCardNo.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     * 
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    private static Pattern pattern1 = Pattern.compile("[0-9]");

    private static Pattern pattern2 = Pattern.compile("[a-z]");

    private static Pattern pattern3 = Pattern.compile("[A-Z]");

    private static Pattern pattern4 = Pattern.compile("[`~!@#$^&*()_=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");

    /**
     * 检测 用户密码强度
     * 
     * @param pwd
     * @return
     */
    public static boolean checkUserPwd(String pwd) {
        if (pwd == null || pwd.indexOf(" ") != -1) {
            return false;
        }
        int levelNum = 0;
        if (pattern1.matcher(pwd).find()) {
            levelNum++;
        }
        if (pattern2.matcher(pwd).find()) {
            levelNum++;
        }
        if (pattern3.matcher(pwd).find()) {
            levelNum++;
        }
        if (pattern4.matcher(pwd).find()) {
            levelNum++;
        }
        if (levelNum < 2) {
            return false;
        }
        return true;
    }

    /**
     * 将回车 ，等处理成<br>
     * 格式
     * 
     * @return
     */
    public static String enterToBr(Object o) {
        if (o == null)
            return "";
        String str = o.toString();
        StringTokenizer st = new StringTokenizer(str, ",\r\n. ");
        String str2 = "";
        int i = 0;
        while (st.hasMoreTokens()) {
            if (i == 0)
                str2 += st.nextToken() + "<br>";
            else
                str2 += st.nextToken();
            i++;
        }
        return str2;
    }

    public static boolean isPayPwdLength(String pwd) {
        if (!StringUtil.isEmpty(pwd)) {
            int pwdL = pwd.length();
            if (pwdL > 7 && pwdL < 21) {
                return true;
            }
        }
        return false;
    }

    /**
       * 
       * @param date1
       * @param date2
       * @return
       */
    public static int compare_date(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static boolean regAmt(String str) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$");
        if (StringUtil.isNullOrEmpty(str)) {
            return false;
        } else {
            Matcher matcher = pattern.matcher(str);
            return matcher.matches();
        }
    }

    public static void main(String[] args) {
        System.out.println(enterToBr("ada ,adfa"));
        // String uname = "xxx";
        // String password = "xxxxxx";
        // String str = StringUtil.md5(password);
        // str = StringUtil.md5(uname + str);
        // System.out.println(str);
        // //CA code
        // str = StringUtil.md5(uname + str);
        // System.out.println(str);
        // String divString =
        // "尊敬的用户，您于2014-04-27 20:12充值9999.00元已到账。<div style='margin-left:650px;padding-top:100px'>民生易贷团队</div><br/><div style='margin-left:650px;margin-top:-10px'>2014年04月27日</div>";
        // divString = removeStyleChar(divString);
        // System.out.println(divString);
    }
}
